﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Vit.Common;
using Vit.Web;
using Vit.Web.Models;
using Vit.Web.Providers.Data;
using Vit.Web.Helpers;

namespace Vit.Web.SqlProviders
{
    public class SqlLocalizationItemProvider : DbContextDataProvider<WebProvidersDb, LocalizationItem>
    {
        protected override IQueryable<LocalizationItem> OrderByDefault(IQueryable<LocalizationItem> query)
        {
            return query.OrderBy(o => o.CultureInfo).ThenBy(o => o.AssemblyName).ThenBy(o => o.Name);
        }

        public override object[] GetKeys(LocalizationItem obj)
        {
            return new object[] { obj.AssemblyName, obj.CultureInfo, obj.Name };
        }

        public override LocalizationItem GetByKeys(params object[] ids)
        {
            if (ids == null || ids.Length != 3)
                return null;
            string key1 = ids[0].ToString();
            string key2 = ids[1].ToString();
            string key3 = ids[2].ToString();
            return db.Localization.FirstOrDefault(o => o.AssemblyName == key1 && o.CultureInfo == key2 && o.Name == key3);
        }
    }
}
